package com.zy.yun.utils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.yun.base.constant.Const;
import com.zy.yun.base.constant.StatusEnum;
import com.zy.yun.base.entity.Organization;
import com.zy.yun.base.entity.Res;
import com.zy.yun.base.entity.RoleOrganization;
import com.zy.yun.base.entity.RoleRes;
import com.zy.yun.provider.service.IOrganizationService;
import com.zy.yun.provider.service.IResService;
import com.zy.yun.provider.service.IRoleOrganizationService;
import com.zy.yun.provider.service.IRoleResService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: 王彦委
 * @Description: 把数据库中的表缓存到redis中
 * @Date: 21:17 2019/11/7
 */
@Component
public class TableCacheUtil {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IResService resService;

    @Autowired
    private IRoleResService roleResService;
    @Autowired
    private IRoleOrganizationService roleOrganizationService;
    @Autowired
    private IOrganizationService organizationService;

    /**
      * @Author: 王彦委
      * @Description: 设置 表 sys_res,sys_role_res 缓存到redis中
      * @Date: 22:56 2019/11/7
      */
    public  void setTableCache(){
        //只查询“启用”的资源
        EntityWrapper<Res> resQuery = new EntityWrapper<Res>();
        resQuery.eq("status", StatusEnum.STATUS_START.getCode());
        List<Res> resList = resService.selectList(null);
        List<RoleRes> roleResList = roleResService.selectList(null);
        List<RoleOrganization> roleOrganizationList = roleOrganizationService.selectList(null);
        List<Organization> organizationList = organizationService.selectList(null);

        // 把这几张表放入redis缓存中
        redisUtil.setMapObject(Const.DATABASE_NAME, Const.TABLE_SYS_RES, resList);
        redisUtil.setMapObject(Const.DATABASE_NAME, Const.TABLE_SYS_ROLE_RES, roleResList);
        redisUtil.setMapObject(Const.DATABASE_NAME, Const.TABLE_SYS_ROLE_ORGANIZATION, roleOrganizationList);
        redisUtil.setMapObject(Const.DATABASE_NAME, Const.TABLE_SYS_ORGANIZATION, organizationList);


    }

    /**
      * @Author: 王彦委
      * @Description: 更新 sys_res表缓存
      * @Date: 13:22 2019/11/8 0008
      */
    public void updateTableSysRes() {
        //只查询“启用”的资源
        EntityWrapper<Res> resQuery = new EntityWrapper<Res>();
        resQuery.eq("status", StatusEnum.STATUS_START.getCode());
        List<Res> resList = resService.selectList(null);
        redisUtil.setMapObject(Const.DATABASE_NAME, Const.TABLE_SYS_RES, resList);
    }

    /**
      * @Author: 王彦委
      * @Description: 更新 sys_role_res 到缓存中
      * @Date: 13:23 2019/11/8 0008
      */
    public void updateTableSysRoleRes() {
        List<RoleRes> roleResList = roleResService.selectList(null);

        // 把这几张表放入redis缓存中
        redisUtil.setMapObject(Const.DATABASE_NAME, Const.TABLE_SYS_ROLE_RES, roleResList);
    }

    /**
      * @Author: 王彦委
      * @Description: 更新 sys_role_organization 到缓存中
      * @Date: 11:32 2019/11/10
      */
    public void updateTableSysRoleOrganization() {
        List<RoleOrganization> roleOrganizationList = roleOrganizationService.selectList(null);
        redisUtil.setMapObject(Const.DATABASE_NAME, Const.TABLE_SYS_ROLE_ORGANIZATION, roleOrganizationList);
    }

    /**
      * @Author: 王彦委
      * @Description: 更新 sys_organization 到缓存中
      * @Date: 11:43 2019/11/10
      */

    public void updateTableSysOrganization() {
        List<Organization> organizationList = organizationService.selectList(null);
        redisUtil.setMapObject(Const.DATABASE_NAME, Const.TABLE_SYS_ORGANIZATION, organizationList);
    }
}
